﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Brewmaster
{
    public partial class MainForm : Form, ILocalizable
    {

        private LocalizationManager localizationManager;

        public MainForm()
        {
            InitializeComponent();
            localizationManager = new LocalizationManager(this);
            if (Brewmaster.Properties.Settings.Default.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                this.Size = Brewmaster.Properties.Settings.Default.Size;
            }
            manageResize();
            inventorySplitContainer.SplitterDistance = Brewmaster.Properties.Settings.Default.InventoryPanelSplitterDistance;
            localizationManager.localize(this);
            inventoryMenuButton.radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            inventorySplitContainer.SplitterMoved+=new SplitterEventHandler(splitContainer1_SplitterMoved);
        }

        void splitContainer1_SplitterMoved(object sender, EventArgs e)
        {
            Brewmaster.Properties.Settings.Default.InventoryPanelSplitterDistance = inventorySplitContainer.SplitterDistance;
        }

        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (inventoryMenuButton.radioButton.Checked)
            {
                // A bug (?) in SplitterContainer causes it to change
                // SplitterDistance upon becoming visible occasionally
                // so we'll buffer the correct value and set it afterwards.
                int splitterDistance = Brewmaster.Properties.Settings.Default.InventoryPanelSplitterDistance;
                inventoryPanel.Visible = true;
                inventorySplitContainer.SplitterDistance = splitterDistance;
            }
            else
            {
                inventoryPanel.Visible = false;
            }
        }

        /// <summary>
        /// Carries out localization according to the current culture information
        /// </summary>
        public void localize()
        {
            inventoryMenuButton.Label = Brewmaster.Properties.Resources.inventoryMenuButtonText;
            recipesMenuButton.Label = Brewmaster.Properties.Resources.recipesMenuButtonText;
            diaryMenuButton.Label = Brewmaster.Properties.Resources.diaryMenuButtonText;
        }

        private void manageResize()
        {
            if (this.ClientSize.Width >= menuPanel.Width)
            {
                menuPanel.Left = (this.ClientSize.Width - menuPanel.Width) / 2;
            }
            else
            {
                menuPanel.Left = 0;
            }
            inventoryPanel.Height = this.ClientSize.Height - menuPanel.Top - menuPanel.Height - menuPanel.Padding.Bottom;
            if (this.WindowState == FormWindowState.Maximized)
            {
                Brewmaster.Properties.Settings.Default.WindowState = FormWindowState.Maximized;
            }
            else
            {
                Brewmaster.Properties.Settings.Default.WindowState = FormWindowState.Normal;
            }
            Brewmaster.Properties.Settings.Default.Size = this.Size;
        }

        private void saveSettings()
        {
            Brewmaster.Properties.Settings.Default.Save();
        }

        private void initializeMenu()
        {

        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            manageResize();
        }

        private void MainForm_FormClosing(object sender, EventArgs e)
        {
            saveSettings();
        }
    }
}
